import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class App {
    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                Integer w = 20;
                Integer h = 10;
                Integer mineCount = 10;

                // 创建逻辑
                var ms = new Minesweeper();
                ms.StartGame(w, h, mineCount);

                // 创建主窗口
                JFrame frame = new JFrame("扫雷");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);

                // 创建主体Panel并调整窗口大小以适应主体Panel
                var panel = new GamePanel(ms);
                frame.add(panel);
                var size = panel.getSize();
                frame.setVisible(true);
                var insets = frame.getInsets();
                frame.setSize(size.width + insets.left + insets.right, size.height + insets.top + insets.bottom);

                // 添加输入监听
                panel.addMouseListener(new MouseListener(){
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        // pass
                    }
        
                    @Override
                    public void mousePressed(MouseEvent e) {
                        //pass
                    }
        
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        var ret = panel.MouseReleased(e.getPoint(), e.getButton());
                        if(ret == 1){
                            JOptionPane.showMessageDialog(panel, "胜利", "扫雷", JOptionPane.PLAIN_MESSAGE);
                            System.exit(0);
                        }else if(ret == -1){
                            JOptionPane.showMessageDialog(panel, "失败", "扫雷", JOptionPane.PLAIN_MESSAGE);
                            System.exit(0);
                        }
                    }
        
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        // pass
                    }
        
                    @Override
                    public void mouseExited(MouseEvent e) {
                        // pass
                    }
                    
                });

                panel.addMouseMotionListener(new MouseMotionListener(){
                    @Override
                    public void mouseDragged(java.awt.event.MouseEvent e) {
                        // pass
                    }

                    @Override
                    public void mouseMoved(java.awt.event.MouseEvent e) {
                        panel.MouseMotion(e.getPoint());
                    }
                });
            }
        });
    }
}
